<?php
/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


class IconControl extends CWidget {

    /**
     * array of icons for the widgets
     *
     * attribute is associate in array with $key=>$value
     *
     * $key available are
     * title -> Tooltip of the icon
     * value -> Text/value of the icon
     * url -> The URL link of the icon. default is set to '#'
     * icon -> The CSS class name of the jquery icon
     * click -> Icon click event. 
     *
     * @var array
     */
    public $icons = array();

    public $styleID ='';

    private $baseUrl;
    
    public function run() {
        $this->baseUrl = Yii::getPathOfAlias('application.widgets.iconcontrol');
        $id = $this->getId();
        if(count($this->icons)>0) {
            $js='';
            if(!empty($this->styleID))
                $this->styleID = 'class="'.$this->styleID.'"';
            else
                $this->styleID = 'class="jquery_ui_icon"';
            $html = '<div id="jquery_ui_icon" '.$this->styleID.'><ul id="jquery_ui_icon" class="ui-widget" >';
            foreach($this->icons as $iconid=>$attribute) {
                $iconid = $id.'_'.$iconid;
                if(!isset($attribute['title']))
                    $attribute['title'] = '';
                if(!isset($attribute['value']))
                    $attribute['value']='';
                if(!isset($attribute['icon']))
                    $attribute['icon']='';
                if(!isset($attribute['url']))
                    $attribute['url'] = 'javascript:void(0);';
                if(isset($attribute['click'])) {
                    $js .= "$('#icon_{$iconid}').bind('click',{$attribute['click']});";
                }
                $html .= "<li class=\"ui-state-default ui-corner-all\" title=\"{$attribute['title']}\"><a id=\"icon_{$iconid}\" href=\"{$attribute['url']}\"><span class=\"ui-icon {$attribute['icon']} \"></span>{$attribute['value']}</a></li>";
            }
            $html .= '</ul></div>';
            $this->publishAssets($js);
            echo $html;
        }
    }
    private function publishAssets($js='') {
        $publishurl = Yii::app()->getAssetManager()->publish($this->baseUrl);
        $cs = Yii::app()->getClientScript();

        $cs->registerCssFile($publishurl.'/css/IconControl.css');
        $cs->registerScriptFile($publishurl.'/js/IconControl.js');

        if(!empty($js)) {
            $cs->registerScript(__CLASS__.'#'.$this->getId(true), $js,CClientScript::POS_READY);
        }
    }
}
?>
